﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "ControlDefinitionLibrary.h"
#include "FindByIdPredicate.h"
#include "ControlDefinitionLibraryEvent.h"
#include "GbmpAssert.h"
#include "ControlDefinitionUtil.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

namespace gcmp
{

ControlDefinitionLibrary::ControlDefinitionLibrary()
{ 
    m_pDefinitionLibraryEvent = NEW_AS_OWNER_PTR(ControlDefinitionLibraryEvent, L"ControlDefinitionLibraryEvent", this);
}

ControlDefinitionLibrary::~ControlDefinitionLibrary()
{
   
}

IControlDefinition* ControlDefinitionLibrary::GetControlDefinition(const std::wstring& id) const
{
    auto search = std::find_if(m_controlDefines.begin(), m_controlDefines.end(), FindPtrBIdPredicate<OwnerPtr<IControlDefinition>>(id));
    return (search != m_controlDefines.end()) ? const_cast<IControlDefinition*>(search->get()) : nullptr;
}

bool ControlDefinitionLibrary::DeleteControlDefinition(const std::wstring& id)
{
    auto search = std::find_if(m_controlDefines.begin(), m_controlDefines.end(), FindPtrBIdPredicate<OwnerPtr<IControlDefinition>>(id));
    DBG_WARN_AND_RETURN_FALSE_UNLESS(search != m_controlDefines.end(), L"删除的对象不存在",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(*search, L"IControlDefinition为空",L"GDMPLab",L"2024-03-30");

    ControlDefinitionType type = (*search)->GetType();

    ControlDefinitionLibraryEventArgs args(id, type);
    m_pDefinitionLibraryEvent->Emit(&args);

    m_controlDefines.erase(search);
    return true;
}

gcmp::IControlDefinitionLibraryEvent* ControlDefinitionLibrary::GetEvent()
{
    return m_pDefinitionLibraryEvent.get();
}

const std::vector<const IControlDefinition *> ControlDefinitionLibrary::GetAllIControlDefinition() const
{
    std::vector<const IControlDefinition *> allControlDefines;
    allControlDefines.resize(m_controlDefines.size());
    FOR_EACH_INDEX(itemIndex, m_controlDefines)
    { 
        allControlDefines[itemIndex] = m_controlDefines[itemIndex].get();
    }
    return allControlDefines;
}

gcmp::IControlDefinitionChangeEvent* ControlDefinitionLibrary::GetControlDefinitionChangeEvent(const ILineEditDefinition* pLineEditDefinition)
{
    return ControlDefinitionUtil::GetControlDefinitionChangeEvent(pLineEditDefinition);
}

gcmp::IControlDefinitionChangeEvent* ControlDefinitionLibrary::GetControlDefinitionChangeEvent(const IComboBoxDefinition* pComboBoxDefinition)
{
    return ControlDefinitionUtil::GetControlDefinitionChangeEvent(pComboBoxDefinition);
}

gcmp::IControlDefinitionChangeEvent* ControlDefinitionLibrary::GetControlDefinitionChangeEvent(const ICheckBoxGroupDefinition* pCheckBoxGroupDefinition)
{
    return ControlDefinitionUtil::GetControlDefinitionChangeEvent(pCheckBoxGroupDefinition);
}

gcmp::IControlDefinitionChangeEvent* ControlDefinitionLibrary::GetControlDefinitionChangeEvent(const IRadioButtonGroupDefinition* pRadioButtonGroupDefinition)
{
    return ControlDefinitionUtil::GetControlDefinitionChangeEvent(pRadioButtonGroupDefinition);
}

gcmp::IControlDefinitionChangeEvent* ControlDefinitionLibrary::GetControlDefinitionChangeEvent(const IProgressBarDefinition* pProgressBarDefinition)
{
    return ControlDefinitionUtil::GetControlDefinitionChangeEvent(pProgressBarDefinition);
}

gcmp::IControlDefinitionChangeEvent* ControlDefinitionLibrary::GetControlDefinitionChangeEvent(const ICommandButtonDefinition* pCommandButtonDefinition)
{
    return ControlDefinitionUtil::GetControlDefinitionChangeEvent(pCommandButtonDefinition);
}

gcmp::IControlDefinitionChangeEvent* ControlDefinitionLibrary::GetControlDefinitionChangeEvent(const IGalleryDefinition* pGalleryDefinition)
{
    return ControlDefinitionUtil::GetControlDefinitionChangeEvent(pGalleryDefinition);
}

gcmp::IControlDefinitionChangeEvent* ControlDefinitionLibrary::GetControlDefinitionChangeEvent(const ILabelDefinition* pLabelDefinition)
{
    return ControlDefinitionUtil::GetControlDefinitionChangeEvent(pLabelDefinition);
}

gcmp::IControlDefinitionChangeEvent* ControlDefinitionLibrary::GetControlDefinitionChangeEvent(const ICommandButtonMenuDefinition* pCommandButtonMenuDefinition)
{
    return ControlDefinitionUtil::GetControlDefinitionChangeEvent(pCommandButtonMenuDefinition);
}

bool ControlDefinitionLibrary::IsReference(const IControlDefinition* pControlDef)
{
    return ControlDefinitionUtil::IsReference(pControlDef);
}

bool ControlDefinitionLibrary::AddControlDefinition(OwnerPtr<IControlDefinition> pControlDefinition)
{
    auto search = std::find_if(m_controlDefines.begin(), m_controlDefines.end(), FindPtrBIdPredicate<OwnerPtr<IControlDefinition>>(pControlDefinition->GetId()));
    DBG_WARN_AND_RETURN_FALSE_UNLESS(search == m_controlDefines.end(), L"已经存在id相同的定义",L"GDMPLab",L"2024-03-30");
    m_controlDefines.push_back(TransferOwnership(pControlDefinition));
    return true;
}

}
